#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"
#include "testutils.h"

void ambassador_test()
{
  int i, k, j, c, pass, tempCoin, choice1, choice2, tempCard, numPlayers, coin_bonus;
  struct gameState state;

  for(i = 0; i < MAX_ITERATIONS; i++){
    coin_bonus = 0;
    pass = 0;

    numPlayers = 4;

    init_test_data(&state, numPlayers);

    srand(time(NULL));

    for(k = 0; k < numPlayers; k++){
      // set deck
      state.deckCount[k] = 50;
      for(j = 0; j < state.deckCount[k]; j++){
        state.deck[k][j] = random_card();
      }

      // set hand
      state.handCount[k] = 5;
      for(j = 0; j < state.handCount[k]; j++){
        state.hand[k][j] = random_card();
      }
    }
    state.hand[0][4] = ambassador;

    // make sure player has the card
    c = rand() % 5;
    choice1 = state.hand[0][c];
    
    // boundaries included
    choice2 = (rand() % 4) - 1;

    printf("ITERATION %d\n", i);

    // choice1 == card to reveal, choice2 == how many of that card to return
    if(cardEffect(state.hand[0][4], choice1, choice2, -1, &state, 4, &coin_bonus) == -1){
      if(choice2 > 2 || choice2 < 0)
	pass++;
      else if (choice1 == 4)
	pass++;
      else if (choice2 != card_count(state.hand[0], choice1, state.handCount[0]))
	pass++;
      else
	printf("Failure, effect did not return error when given incorrect choices\n");
    } else
      pass++;

    for(k = 1; k < numPlayers; k++){
      if(state.hand[k][5] == choice1)
	pass++;
      else
	printf("Failure, player %d did not gain revealed card\n", k + 1);
    }

    if(state.hand[0][4] == ambassador)
      printf("Failure, player did not discard ambassador card\n");
    else
      pass++;

    if(pass == 3)
      printf("Success, all tests passed for ambassador effect\n");
  }
}

int main()
{
  ambassador_test();
  return 0;
}
